package com.noble.meether.chat.ui.agora.act.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.noble.library.image.ImageLoader;
import com.noble.meether.R;
import com.noble.glamour.module.logic.application.App;
import com.noble.meether.chat.ui.agora.act.bean.Props;

import java.util.ArrayList;
import java.util.List;

/**
 * FaceU道具弹框
 * <p>
 * Created by Su on 2017/7/20.
 */
public class RtcFaceUnityAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<Props> mLists; // 单独每个viewPage页礼物列表
    private RtcFaceUnityDialog dialog;
    private View view;
    private int position;

    public RtcFaceUnityAdapter(Context fContext, RtcFaceUnityDialog dialog, List<Props> lGift, int page, int pSize) {
        this.mContext = fContext;
        this.dialog = dialog;
        this.inflater = LayoutInflater.from(fContext);
        mLists = new ArrayList<>();
        //计算本页礼物列表
        int start = page * pSize;
        int end = start + pSize;
        while ((start < lGift.size()) && (start < end)) {
            mLists.add(lGift.get(start));
            start++;
        }
    }

    @Override
    public int getCount() {
        return mLists.size();
    }

    @Override
    public Object getItem(int position) {
        return mLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder vh;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.f1_face_unity_dialog_item, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        if (mLists != null && mLists.size() > 0) {
            final Props info = mLists.get(position);

            // 加载首个item
            if (info.isFirst()) {
                vh.img.setImageResource(R.drawable.f1_video_prop_no);
                vh.tvCost.setTextColor(App.getActivity().getResources().getColor(R.color.white));
                vh.tvCost.setText(App.activity.getString(R.string.chat_face_reset));
                vh.tvName.setText(App.activity.getString(R.string.chat_face_cancel));
            }
            // 加载道具信息
            else {
                vh.tvName.setText(info.getName());
                if (info.getPrice() != 0) {
                    vh.tvCost.setText(App.activity.getString(R.string.goods_diamond_need, info.getPrice()));
                }
                ImageLoader.loadCenterCrop(mContext, info.getIcon(), vh.img, R.drawable.default_props, R.drawable.default_props);

            }
            vh.llTop.setOnClickListener(v -> {
                view = v;
                RtcFaceUnityAdapter.this.position = position;
                dialog.onItemClick(null, null, position, 0);
            });
        }
        return convertView;
    }

    public View getView() {
        return view;
    }

    public int getPosition() {
        return position;
    }

    class ViewHolder {
        ImageView img;//礼物图像
        TextView tvName, tvCost;//礼物名称，需要的钻石
        LinearLayout llTop;//用于控制选中或未选中的背景

        public ViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            img = convertView.findViewById(R.id.face_item_img);
            tvName = convertView.findViewById(R.id.face_item_name);
            tvCost = convertView.findViewById(R.id.face_item_cost);
            llTop = convertView.findViewById(R.id.face_item_ll);
        }
    }
}